package com.glory.brepda.activity.lanjian;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.activity.mfg.BreHoldsEntity;
import com.glory.brepda.adapter.BreHoldFlowAdapter;
import com.glory.brepda.adapter.SampleListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.SFCStockingEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BreHoldFolwActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.rvBINList)
    RecyclerView rvBINList;

    private BreHoldFlowAdapter mBreHoldFlowAdapter;
    private List<BreHoldsEntity.HoldEntity> body;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bre_hold_folw;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setVisibility(View.VISIBLE);
        initRecyclerView(rvBINList, this);
        mBreHoldFlowAdapter = new BreHoldFlowAdapter(null);
        mBreHoldFlowAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mBreHoldFlowAdapter.isFirstOnly(true);
        rvBINList.setAdapter(mBreHoldFlowAdapter);

    }

    @Override
    protected void initData() {
        initEdit(etLotID, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<BreHoldsEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<BreHoldsEntity>>
                    ("正在查询LOT信息", new TypeToken<BaseResponseBean<BreHoldsEntity>>() {
                    }.getType(), getSupportFragmentManager(), BreHoldFolwActivity.this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<BreHoldsEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(BreHoldFolwActivity.this, true, getSupportFragmentManager())) {
                        body = response.body().getResponse().getBody().getHolds();
//                        if(body.size()>0){
                            mBreHoldFlowAdapter.setNewData(body);
//                        }else{
//                            mBreHoldFlowAdapter.getData().clear();
//                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<BreHoldsEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "PDA取样");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("LOT_ID", s);
            map.addProperty("ACTION", "QUERY");

            NetUtils.requestPostNet("IF.PDA_HOLD_SAMPLE_LOT_QTY", "Common", BreHoldFolwActivity.this, map, callBack);
        });
    }

//    public void getLotFlowDate(){
//        FreeUI_EntityCallBack<BaseResponseBean<BreHoldsEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<BreHoldsEntity>>
//                ("正在查询LOT信息", new TypeToken<BaseResponseBean<BreHoldsEntity>>() {
//                }.getType(), getSupportFragmentManager(), BreHoldFolwActivity.this) {
//
//            @Override
//            public void onSuccess(Response<BaseResponseBean<BreHoldsEntity>> response) {
//                super.onSuccess(response);
//                if (response.body().isSuccess(BreHoldFolwActivity.this, true, getSupportFragmentManager())) {
//                    body = response.body().getResponse().getBody().getHolds();
//                    if(body.size()>0){
//                        mBreHoldFlowAdapter.setNewData(body);
//                    }else{
//                        mBreHoldFlowAdapter.getData().clear();
//                    }
//                }
//            }
//
//            @Override
//            public void onError(Response<BaseResponseBean<BreHoldsEntity>> response) {
//                super.onError(response);
//                loadError(response.getException(), "PDA取样");
//            }
//        };
//
//        JsonObject map = new JsonObject();
//        map.addProperty("LOT_ID", getText(etLotID));
//        map.addProperty("ACTION", "QUERY");
//
//        NetUtils.requestPostNet("IF.PDA_HOLD_SAMPLE_LOT_QTY", "Common", BreHoldFolwActivity.this, map, callBack);
//    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }

    private void submit(){
        // 只保留勾选项
        List<BreHoldsEntity.HoldEntity> selectItems = new ArrayList<>();

        if(mBreHoldFlowAdapter.getData().size() == 0){
            showToast("无需取样项！", false);
            return;
        }

        for(BreHoldsEntity.HoldEntity item :mBreHoldFlowAdapter.getData()){
            if(item.isChecked()){
                selectItems.add(item);
            }
        }
        if(selectItems.size() == 0){
            showToast("请选择取样的项！", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<BreHoldsEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BreHoldsEntity>>
                (new TypeToken<BaseResponseBean<BreHoldsEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BreHoldsEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("取样完成",false);
                    // 重新赋值
                    List<BreHoldsEntity.HoldEntity> newDates = new ArrayList<>();
                    newDates.addAll(mBreHoldFlowAdapter.getData());
                    newDates.removeAll(selectItems);
                    mBreHoldFlowAdapter.setNewData(newDates);
//                    mBreHoldFlowAdapter.getData().removeAll(selectItems);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BreHoldsEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "BRE_NONPAPER");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOT_ID", getText(etLotID));
        map.addProperty("ACTION", "SAVE");
        map.add("ITEMS", new JsonParser().parse(new Gson().toJson(selectItems)).getAsJsonArray());

        NetUtils.requestPostNet("IF.PDA_HOLD_SAMPLE_LOT_QTY", "Common", BreHoldFolwActivity.this, map, callBack);
    }
}